/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.Controller;

import admin.BusinessLogic.BusinessLogic;
import admin_gui.Admin_EditOrder;
import admin_gui.Admin_SearchOrder;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author WEN
 */
public class OrderController {

    private BusinessLogic businesslogic;
    private JPanel mainPanel;
    private Vector order;
    private Vector vector;
    private boolean[] orderItem;
    private int deleteType = 0;             //to remember the selection type before deleting
    private String searchSelection = "";

    public OrderController(JPanel MainPanel) {
        try {
            //      System.out.println("in item controller1");
            businesslogic = new BusinessLogic();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(OrderController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(OrderController.class.getName()).log(Level.SEVERE, null, ex);
        }
        mainPanel = MainPanel;
        order = new Vector();
        orderItem = new boolean[4];
        orderItem[0] = true;
        for (int i = 1; i < 4; i++) {
            orderItem[i] = false;
        }
    }

    public void manageOrder(JTable ResultTable, Vector v_head,
            String searchField, boolean[] orderItem) {
    //    System.out.println("searchField:" + searchField);
        this.orderItem = orderItem;
        if (orderItem[0] == true || searchField.length() == 0) {
     //       System.out.println("No selection: " + searchField.trim());
            showOrderTable(ResultTable, v_head);
        } else if (orderItem[1] == true) {
            showOrderTableByID(searchField.trim(), ResultTable, v_head);
            deleteType = 1;
            searchSelection = searchField.trim();
        } else if (orderItem[2] == true) {
            showOrderTableByDate(searchField.trim(), ResultTable, v_head);
            deleteType = 2;
            searchSelection = searchField.trim();
        } else if (orderItem[3] == true) {
            showOrderTableByStatus(searchField.trim(), ResultTable, v_head);
            deleteType = 3;
            searchSelection = searchField.trim();
        } else {
            JOptionPane.showMessageDialog(null,
                    "Sorry! Can not find that information",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showOrderTable(JTable ResultTable, Vector v_head) {
        order.clear();
        order = businesslogic.getAllOrder();
        if (order == null) {
            order = new Vector();
        }
        DefaultTableModel tm = new DefaultTableModel(order, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showOrderTableByID(String ID, JTable ResultTable, Vector v_head) {
        order.clear();
        order = businesslogic.getOrderByID(ID);
        if (order == null) {
            order = new Vector();
        }
        DefaultTableModel tm = new DefaultTableModel(order, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showOrderTableByDate(String Date, JTable ResultTable,
            Vector v_head) {
        order.clear();
        order = businesslogic.getOrderByDate(Date);
        DefaultTableModel tm = new DefaultTableModel(order, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showOrderTableByStatus(String status, JTable ResultTable,
            Vector v_head) {
        order.clear();
        order = businesslogic.getOrderByStatus(status);
        DefaultTableModel tm = new DefaultTableModel(order, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void showEditOrderPanel(String orderID) {
        vector = businesslogic.getOrderByID(orderID);
        Admin_EditOrder editOrderPane = new Admin_EditOrder(this, vector);
        editOrderPane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(editOrderPane);
        mainPanel.validate();
        mainPanel.repaint();
    }

    public void updateOrderstatus(int id, String status) {
        if (businesslogic.updateOrderstatus(id, status)) {
            JOptionPane.showMessageDialog(null,
                    "The order status has been updated!",
                    "Message", JOptionPane.INFORMATION_MESSAGE);
            Admin_SearchOrder searchOrderPane = new Admin_SearchOrder(mainPanel);
            searchOrderPane.setBounds(1, 1, 762, 613);
            mainPanel.removeAll();
            mainPanel.add(searchOrderPane);
            mainPanel.validate();
            mainPanel.repaint();

        }
    }

    public void goBackSerachOrderPane() {
        Admin_SearchOrder searchOrderPane = new Admin_SearchOrder(mainPanel);
        searchOrderPane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(searchOrderPane);
        mainPanel.validate();
        mainPanel.repaint();
    }
}
